package com.cy.aircompressor.main.home.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.system.SysConfig;
import com.cy.aircompressor.entity.system.UserInfo;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.util.List;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/11/12 or 23:27
 * 描述：---
 */
public class MianViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<UserInfo>> userInfoObservable;
    private MediatorLiveData<Resource<SysConfig>> sysConfigObservable;
    private MediatorLiveData<Resource<Integer>> warningObservable;
    private MediatorLiveData<Resource<Object>> updatePwdObservable;
    private MediatorLiveData<Resource<Object>> logoutObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> PartObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> PointObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> compressorObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> collectorChannelObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> powerStationObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> sensorRangeObservable;
    private MediatorLiveData<Resource<List<SelectDict>>> sensorTypeObservable;

    public MianViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        userInfoObservable = new MediatorLiveData<>();
        sysConfigObservable = new MediatorLiveData<>();
        warningObservable = new MediatorLiveData<>();
        PartObservable = new MediatorLiveData<>();
        PointObservable = new MediatorLiveData<>();
        compressorObservable = new MediatorLiveData<>();
        collectorChannelObservable = new MediatorLiveData<>();
        powerStationObservable = new MediatorLiveData<>();
        sensorRangeObservable = new MediatorLiveData<>();
        sensorTypeObservable = new MediatorLiveData<>();
        updatePwdObservable = new MediatorLiveData<>();
        logoutObservable = new MediatorLiveData<>();
    }

    public void currentUserInfo() {
        userInfoObservable.addSource(getRepository().currentUserInfo(), new Observer<Resource<UserInfo>>() {
            @Override
            public void onChanged(Resource<UserInfo> userInfoResource) {
                userInfoObservable.setValue(userInfoResource);
            }
        });
    }

    public void updatePwd(Object parameters) {
        updatePwdObservable.addSource(getRepository().updatePwd(parameters), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> userInfoResource) {
                updatePwdObservable.setValue(userInfoResource);
            }
        });
    }

    public void logout() {
        logoutObservable.addSource(getRepository().logout(), new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> userInfoResource) {
                logoutObservable.setValue(userInfoResource);
            }
        });
    }


    public MediatorLiveData<Resource<UserInfo>> getUserInfoObservable() {
        return userInfoObservable;
    }

    public void queryConfig() {
        sysConfigObservable.addSource(getRepository().queryConfig(), new Observer<Resource<SysConfig>>() {
            @Override
            public void onChanged(Resource<SysConfig> sysConfigResource) {
                sysConfigObservable.setValue(sysConfigResource);
            }
        });
    }

    public MediatorLiveData<Resource<SysConfig>> getSysConfigObservable() {
        return sysConfigObservable;
    }

    public void queryWarningCount() {
        warningObservable.addSource(getRepository().queryWarningCount(), new Observer<Resource<Integer>>() {
            @Override
            public void onChanged(Resource<Integer> sysConfigResource) {
                warningObservable.setValue(sysConfigResource);
            }
        });
    }

    public MediatorLiveData<Resource<Integer>> getWarningObservable() {
        return warningObservable;
    }

    public void compressorPart() {
        PartObservable.addSource(getRepository().compressorPart(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                PartObservable.setValue(sysConfigResource);
            }
        });
    }

    public void compressorPoint() {
        PointObservable.addSource(getRepository().compressorPoint(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                PointObservable.setValue(sysConfigResource);
            }
        });
    }

    public void compressor() {
        compressorObservable.addSource(getRepository().compressor(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                compressorObservable.setValue(sysConfigResource);
            }
        });
    }

    public void collectorChannel() {
        collectorChannelObservable.addSource(getRepository().collectorChannel(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                collectorChannelObservable.setValue(sysConfigResource);
            }
        });
    }

    public void powerStation() {
        powerStationObservable.addSource(getRepository().powerStation(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                powerStationObservable.setValue(sysConfigResource);
            }
        });
    }

    public void sensorRange() {
        sensorRangeObservable.addSource(getRepository().sensorRange(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                sensorRangeObservable.setValue(sysConfigResource);
            }
        });
    }

    public void sensorType() {
        sensorTypeObservable.addSource(getRepository().sensorType(), new Observer<Resource<List<SelectDict>>>() {
            @Override
            public void onChanged(Resource<List<SelectDict>> sysConfigResource) {
                sensorTypeObservable.setValue(sysConfigResource);
            }
        });
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getPartObservable() {
        return PartObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getPointObservable() {
        return PointObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getCompressorObservable() {
        return compressorObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getCollectorChannelObservable() {
        return collectorChannelObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getPowerStationObservable() {
        return powerStationObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getSensorRangeObservable() {
        return sensorRangeObservable;
    }

    public MediatorLiveData<Resource<List<SelectDict>>> getSensorTypeObservable() {
        return sensorTypeObservable;
    }

    public MediatorLiveData<Resource<Object>> getUpdatePwdObservable() {
        return updatePwdObservable;
    }

    public MediatorLiveData<Resource<Object>> getLogoutObservable() {
        return logoutObservable;
    }
}
